10 //Mouse Control Script using IR
11 //by vkapadia with much assistance from inio
12 //vkapadia@vkapadia.com
15 //To calibrate, run this program and put the Wiimote on a flat surface face-up.
16 //Then read the values in the debug line (next to the run button).
17 //Change these values until the debug line reads approx. all zeros.
38 var.deadzone = 5 //distance in pixels that you have to move the wiimote in
39 //order for it to register movement. Creates a "dead zone" around the pointer
40 //to make it easier to click. Higher = smoother but less accurate.
43 //cursor2.visible = true
45 //more options to be added later
48 //Point Wiimote = Move Mouse
50 //B-Button = Left Click
52 //A-Button = Right Click
53 //Plus and Minus = Control Volume
57 //If the pointer hits the edge of the screen, the Wiimote will rumble a bit.
59 //The LEDs attempt to emulate KITT's grill from Knight Rider
62 //***Do not edit anything below this line unless you know what you are doing.***
63 var.accx1 = wiimote1.RawForceX + var.xtrim1
64 var.accy1 = wiimote1.RawForceY + var.ytrim1
65 var.accz1 = wiimote1.RawForceZ + var.ztrim1
67 if wiimote1.dot1vis and wiimote1.dot2vis then
69 if var.accy1 > -7 then
71 elseif var.accy1 > -45 then
81 if var.leftpoint1 = 0 then
82 if var.orientation1 = 0 then
83 if wiimote1.dot1x < wiimote1.dot2x then
89 if var.orientation1 = 1 then
90 if wiimote1.dot1y > wiimote1.dot2y then
96 if var.orientation1 = 2 then
97 if wiimote1.dot1x > wiimote1.dot2x then
103 if var.orientation1 = 3 then
104 if wiimote1.dot1y < wiimote1.dot2y then
112 if var.leftpoint1 = 1 then
113 var.fix1x1 = wiimote1.dot1x
114 var.fix1y1 = wiimote1.dot1y
115 var.fix2x1 = wiimote1.dot2x
116 var.fix2y1 = wiimote1.dot2y
118 var.fix1x1 = wiimote1.dot2x
119 var.fix1y1 = wiimote1.dot2y
120 var.fix2x1 = wiimote1.dot1x
121 var.fix2y1 = wiimote1.dot1y
124 var.dx1 = var.fix2x1 - var.fix1x1
125 var.dy1 = var.fix2y1 - var.fix1y1
126 var.cx1 = (var.fix1x1+var.fix2x1)/1024.0 - 1
127 var.cy1 = (var.fix1y1+var.fix2y1)/1024.0 - .75
129 var.d1 = sqrt(var.dx1*var.dx1+var.dy1*var.dy1)
131 var.dx1 = var.dx1 / var.d1
132 var.dy1 = var.dy1 / var.d1
134 var.ox1 = -var.dy1*var.cy1-var.dx1*var.cx1;
135 var.oy1 = -var.dx1*var.cy1+var.dy1*var.cx1;
137 var.ax1 = (var.ox1 * var.coeff*screen.desktopwidth) + (screen.desktopwidth* var.coeff / 2)
138 var.ay1 = (-var.oy1* var.coeff * screen.desktopwidth) + (screen.desktopheight* var.coeff / 2)
140 var.dx1 = var.ax1 - cursor2.posx
141 var.dy1 = var.ay1 - cursor2.posy
143 var.d1 = sqrt((var.dx1*var.dx1)+(var.dy1*var.dy1))
145 var.a1 = 180 / (200 + var.d1 * var.d1 * var.d1 * .001)
147 if var.d1 <= var.deadzone then var.a1 = 1
149 //debug = var.d + " " + var.a
151 var.finalx1 = cursor2.posx * var.a1 + var.ax1 * (1 - var.a1)
152 var.finaly1 = cursor2.posy * var.a1 + var.ay1 * (1 - var.a1)
155 cursor2.posx = smooth(var.finalx1,3,5)
156 cursor2.posy = smooth(var.finaly1,3,5)
164 var.xpos1 = var.finalx1
165 var.ypos1 = var.finaly1
166 ppjoy1.analog0 = ensuremaprange(var.xpos1,0,screen.desktopwidth* var.coeff,-1,1)
167 ppjoy1.analog1 = ensuremaprange(var.ypos1,0,screen.desktopheight* var.coeff,-1,1)
169 if wiimote1.B or wiimote1.A or wiimote1.Up or wiimote1.down or wiimote1.Left or wiimote1.Right == true
170 ppjoy1.digital0 = true
172 ppjoy1.digital0 = false
176 //***Do not edit anything below this line unless you know what you are doing.***
177 var.accx2 = wiimote2.RawForceX + var.xtrim2
178 var.accy2 = wiimote2.RawForceY + var.ytrim2
179 var.accz2 = wiimote2.RawForceZ + var.ztrim2
181 if wiimote2.dot1vis and wiimote2.dot2vis then
183 if var.accy2 > -7 then
185 elseif var.accy2 > -45 then
186 if var.accx2 < 0 then
195 if var.leftpoint2 = 0 then
196 if var.orientation2 = 0 then
197 if wiimote2.dot1x < wiimote2.dot2x then
203 if var.orientation2 = 1 then
204 if wiimote2.dot1y > wiimote2.dot2y then
210 if var.orientation2 = 2 then
211 if wiimote2.dot1x > wiimote2.dot2x then
217 if var.orientation2 = 3 then
218 if wiimote2.dot1y < wiimote2.dot2y then
226 if var.leftpoint2 = 1 then
227 var.fix1x2 = wiimote2.dot1x
228 var.fix1y2 = wiimote2.dot1y
229 var.fix2x2 = wiimote2.dot2x
230 var.fix2y2 = wiimote2.dot2y
232 var.fix1x2 = wiimote2.dot2x
233 var.fix1y2 = wiimote2.dot2y
234 var.fix2x2 = wiimote2.dot1x
235 var.fix2y2 = wiimote2.dot1y
238 var.dx2 = var.fix2x2 - var.fix1x2
239 var.dy2 = var.fix2y2 - var.fix1y2
240 var.cx2 = (var.fix1x2+var.fix2x2)/1024.0 - 1
241 var.cy2 = (var.fix1y2+var.fix2y2)/1024.0 - .75
243 var.d2 = sqrt(var.dx2*var.dx2+var.dy2*var.dy2)
245 var.dx2 = var.dx2 / var.d2
246 var.dy2 = var.dy2 / var.d2
248 var.ox2 = -var.dy2*var.cy2-var.dx2*var.cx2;
249 var.oy2 = -var.dx2*var.cy2+var.dy2*var.cx2;
251 var.ax2 = (var.ox2 * var.coeff*screen.desktopwidth) + (screen.desktopwidth* var.coeff / 2)
252 var.ay2 = (-var.oy2* var.coeff * screen.desktopwidth) + (screen.desktopheight* var.coeff / 2)
254 var.dx2 = var.ax2 - cursor3.posx
255 var.dy2 = var.ay2 - cursor3.posy
257 var.d2 = sqrt((var.dx2*var.dx2)+(var.dy2*var.dy2))
259 var.a2 = 180 / (200 + var.d2 * var.d2 * var.d2 * .001)
261 if var.d2 <= var.deadzone then var.a2 = 1
263 //debug = var.d + " " + var.a
265 var.finalx2 = cursor3.posx * var.a2 + var.ax2 * (1 - var.a2)
266 var.finaly2 = cursor3.posy * var.a2 + var.ay2 * (1 - var.a2)
269 cursor3.posx = smooth(var.finalx2,3,5)
270 cursor3.posy = smooth(var.finaly2,3,5)
278 var.xpos2 = var.finalx2
279 var.ypos2 = var.finaly2
280 ppjoy2.analog0 = ensuremaprange(var.xpos2,0,screen.desktopwidth* var.coeff,-1,1)
281 ppjoy2.analog1 = ensuremaprange(var.ypos2,0,screen.desktopheight* var.coeff,-1,1)
283 if wiimote2.B or wiimote2.A or wiimote2.Up or wiimote2.down or wiimote2.Left or wiimote2.Right == true
284 ppjoy2.digital0 = true
286 ppjoy2.digital0 = false
289 debug = var.accx2+ " " + var.accy2+ " " + var.accz2
292 //***Do not edit anything below this line unless you know what you are doing.***
293 var.accx3 = wiimote3.RawForceX + var.xtrim3
294 var.accy3 = wiimote3.RawForceY + var.ytrim3
295 var.accz3 = wiimote3.RawForceZ + var.ztrim3
297 if wiimote3.dot1vis and wiimote3.dot2vis then
299 if var.accy3 > -7 then
301 elseif var.accy3 > -45 then
302 if var.accx3 < 0 then
311 if var.leftpoint3 = 0 then
312 if var.orientation3 = 0 then
313 if wiimote3.dot1x < wiimote3.dot2x then
319 if var.orientation3 = 1 then
320 if wiimote3.dot1y > wiimote3.dot2y then
326 if var.orientation3 = 2 then
327 if wiimote3.dot1x > wiimote3.dot2x then
333 if var.orientation3 = 3 then
334 if wiimote3.dot1y < wiimote3.dot2y then
342 if var.leftpoint3 = 1 then
343 var.fix1x3 = wiimote3.dot1x
344 var.fix1y3 = wiimote3.dot1y
345 var.fix2x3 = wiimote3.dot2x
346 var.fix2y3 = wiimote3.dot2y
348 var.fix1x3 = wiimote3.dot2x
349 var.fix1y3 = wiimote3.dot2y
350 var.fix2x3 = wiimote3.dot1x
351 var.fix2y3 = wiimote3.dot1y
354 var.dx3 = var.fix2x3 - var.fix1x3
355 var.dy3 = var.fix2y3 - var.fix1y3
356 var.cx3 = (var.fix1x3+var.fix2x3)/1024.0 - 1
357 var.cy3 = (var.fix1y3+var.fix2y3)/1024.0 - .75
359 var.d3 = sqrt(var.dx3*var.dx3+var.dy3*var.dy3)
361 var.dx3 = var.dx3 / var.d3
362 var.dy3 = var.dy3 / var.d3
364 var.ox3 = -var.dy3*var.cy3-var.dx3*var.cx3;
365 var.oy3 = -var.dx3*var.cy3+var.dy3*var.cx3;
367 var.ax3 = (var.ox3 * var.coeff*screen.desktopwidth) + (screen.desktopwidth* var.coeff / 2)
368 var.ay3 = (-var.oy3* var.coeff * screen.desktopwidth) + (screen.desktopheight* var.coeff / 2)
370 var.dx3 = var.ax3 - cursor4.posx
371 var.dy3 = var.ay3 - cursor4.posy
373 var.d3 = sqrt((var.dx3*var.dx3)+(var.dy3*var.dy3))
375 var.a3 = 180 / (200 + var.d3 * var.d3 * var.d3 * .001)
377 if var.d3 <= var.deadzone then var.a3 = 1
379 //debug = var.d + " " + var.a
381 var.finalx3 = cursor4.posx * var.a3 + var.ax3 * (1 - var.a3)
382 var.finaly3 = cursor4.posy * var.a3 + var.ay3 * (1 - var.a3)
385 cursor4.posx = smooth(var.finalx3,3,5)
386 cursor4.posy = smooth(var.finaly3,3,5)
394 var.xpos3 = var.finalx3
395 var.ypos3 = var.finaly3
396 ppjoy3.analog0 = ensuremaprange(var.xpos3,0,screen.desktopwidth* var.coeff,-1,1)
397 ppjoy3.analog1 = ensuremaprange(var.ypos3,0,screen.desktopheight* var.coeff,-1,1)
399 if wiimote3.B or wiimote3.A or wiimote3.Up or wiimote3.down or wiimote3.Left or wiimote3.Right == true
400 ppjoy3.digital0 = true
402 ppjoy3.digital0 = false
405 debug = var.accx1+ " " + var.accy1+ " " + var.accz1
408 //***Do not edit anything below this line unless you know what you are doing.***
409 var.accx4 = wiimote4.RawForceX + var.xtrim4
410 var.accy4 = wiimote4.RawForceY + var.ytrim4
411 var.accz4 = wiimote4.RawForceZ + var.ztrim4
413 if wiimote4.dot1vis and wiimote4.dot2vis then
415 if var.accy4 > -7 then
417 elseif var.accy4 > -45 then
418 if var.accx4 < 0 then
427 if var.leftpoint4 = 0 then
428 if var.orientation4 = 0 then
429 if wiimote4.dot1x < wiimote4.dot2x then
435 if var.orientation4 = 1 then
436 if wiimote4.dot1y > wiimote4.dot2y then
442 if var.orientation4 = 2 then
443 if wiimote4.dot1x > wiimote4.dot2x then
449 if var.orientation4 = 3 then
450 if wiimote4.dot1y < wiimote4.dot2y then
458 if var.leftpoint4 = 1 then
459 var.fix1x4 = wiimote4.dot1x
460 var.fix1y4 = wiimote4.dot1y
461 var.fix2x4 = wiimote4.dot2x
462 var.fix2y4 = wiimote4.dot2y
464 var.fix1x4 = wiimote4.dot2x
465 var.fix1y4 = wiimote4.dot2y
466 var.fix2x4 = wiimote4.dot1x
467 var.fix2y4 = wiimote4.dot1y
470 var.dx4 = var.fix2x4 - var.fix1x4
471 var.dy4 = var.fix2y4 - var.fix1y4
472 var.cx4 = (var.fix1x4+var.fix2x4)/1024.0 - 1
473 var.cy4 = (var.fix1y4+var.fix2y4)/1024.0 - .75
475 var.d4 = sqrt(var.dx4*var.dx4+var.dy4*var.dy4)
477 var.dx4 = var.dx4 / var.d4
478 var.dy4 = var.dy4 / var.d4
480 var.ox4 = -var.dy4*var.cy4-var.dx4*var.cx4;
481 var.oy4 = -var.dx4*var.cy4+var.dy4*var.cx4;
483 var.ax4 = (var.ox4 * var.coeff*screen.desktopwidth) + (screen.desktopwidth* var.coeff / 2)
484 var.ay4 = (-var.oy4* var.coeff * screen.desktopwidth) + (screen.desktopheight* var.coeff / 2)
486 var.dx4 = var.ax4 - cursor5.posx
487 var.dy4 = var.ay4 - cursor5.posy
489 var.d4 = sqrt((var.dx4*var.dx4)+(var.dy4*var.dy4))
491 var.a4 = 180 / (200 + var.d4 * var.d4 * var.d4 * .001)
493 if var.d4 <= var.deadzone then var.a4 = 1
495 //debug = var.d + " " + var.a
497 var.finalx4 = cursor5.posx * var.a4 + var.ax4 * (1 - var.a4)
498 var.finaly4 = cursor5.posy * var.a4 + var.ay4 * (1 - var.a4)
501 cursor5.posx = smooth(var.finalx4,3,5)
502 cursor5.posy = smooth(var.finaly4,3,5)
510 var.xpos4 = var.finalx4
511 var.ypos4 = var.finaly4
512 ppjoy4.analog0 = ensuremaprange(var.xpos4,0,screen.desktopwidth* var.coeff,-1,1)
513 ppjoy4.analog1 = ensuremaprange(var.ypos4,0,screen.desktopheight* var.coeff,-1,1)
515 if wiimote4.B or wiimote4.A or wiimote4.Up or wiimote4.down or wiimote4.Left or wiimote4.Right == true
516 ppjoy4.digital0 = true
518 ppjoy4.digital0 = false